//===============================================================================
// Q4Tech Engineering Team
// Mobile Updater Application Block - November 2006
//===============================================================================
// Copyright  Q4Tech Informatica Argentina.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Status;

namespace Q4Tech.Engineering.UpdaterAgent.UpdaterView
{
    public partial class UpdaterViewSP : Form, IUpdaterView
    {
        public event EventHandler ForceUpdate;
        public event EventHandler ViewDeviceID;
        public event EventHandler<BoolEventArgs> VerboseCheck;
        public event EventHandler LoadView;

        public UpdaterViewSP()
        {
            InitializeComponent();
        }

        private void UpdaterView_Load(object sender, EventArgs e)
        {
            if (this.LoadView != null && !IsDesignMode)
                LoadView(sender, e);
        }

        private void verboseCheck_CheckStateChanged(object sender, EventArgs e)
        {
            if (VerboseCheck != null)
                VerboseCheck(sender, new BoolEventArgs(this.verboseCheck.Checked));
        }

        private void forceUpdate_Click(object sender, EventArgs e)
        {
            if (ForceUpdate != null)
                ForceUpdate(sender, e);
        }

        private void closeMenu_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void viewDeviceID_Click(object sender, EventArgs e)
        {
            if (ViewDeviceID != null)
                ViewDeviceID(sender, e);
        }

        #region IUpdaterView Members

 

        public bool ForceUpdateEnabled
        {
            set { UpdateUI(delegate { forceUpdateMenuItem.Enabled = value; }); }
        }

        public void WriteLine(string message)
        {
            UpdateUI(delegate
            {
                txtMessages.Text = "> " + message + "\r\n" +
                    (txtMessages.Text.Length > 2 ? "- " + txtMessages.Text.Substring(2) : "");
                this.Invalidate();
            });
        }

        public void SetStatus(bool connected, int pendingDownloads)
        {
            UpdateUI(delegate
            {
                if (connected)
                {
                    if (pendingDownloads == 0)
                    {
                        statusPicture.Image = Properties.Resources.SmartphoneConnected;
                        statusLabel.Text = Properties.Resources.Connected;
                        forceUpdateMenuItem.Enabled = true;
                    }
                    else
                    {
                        statusPicture.Image = Properties.Resources.SmartphoneDownloading;
                        statusLabel.Text = Properties.Resources.Downloading;
                        forceUpdateMenuItem.Enabled = false;
                    }
                }
                else
                {
                    statusPicture.Image = Properties.Resources.SmartphoneDisconnected;
                    statusLabel.Text = Properties.Resources.Disconnected;
                    forceUpdateMenuItem.Enabled = false;
                }

                this.Update();
                this.Refresh();
            });
        }


        #endregion

        private void UpdateUI(EventHandler action)
        {
            this.Invoke(action);
        }

        private bool IsDesignMode
        {
            get { return Site != null && Site.DesignMode; }
        }
    }
}